package com.example.dao;

import com.example.entity.Assessor;
import com.example.entity.StudentInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 评审人员对象数据访问类
 * @author 李平
 * @date 2022-5-2
 */
@Component
public class AssessorDao {

    @Autowired
    @Qualifier("apiJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    private static final Logger log = LoggerFactory.getLogger(AssessorDao.class.toGenericString());

    /**
     * 通过电话号码和密码查询评审人员信息
     * @param phone
     * @param password
     * @return Assessor
     */
    public Assessor getAssessorByPAndP(String phone, String password){

        String sql = String.format("SELECT * FROM assessor where phone='%s' and password='%s'", phone, password);
        List<Assessor> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Assessor.class));
        log.info(sql);
        return query != null || query.size() > 0 ? query.get(0) : null;

    }

    /**
     * 通过电话号码查询评审人员信息
     * @param phone
     * @return Assessor
     */
    public Assessor getAssessorByPhone(String phone){
        String sql = String.format("SELECT * FROM assessor where phone='%s'", phone);
        List<Assessor> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Assessor.class));
        log.info(sql);
        return query == null || query.size() == 0 ? null : query.get(0);
    }

    /**
     * 插入评审人员对象
     * @param assessor
     * @return Boolean
     */
    public Boolean addAssessor(Assessor assessor){
        String sql = String.format("INSERT INTO assessor(id,phone,password,code) values('%s','%s','%s','%s')",
                assessor.getId(),assessor.getPhone(),assessor.getPassword(),assessor.getCode());
        log.info(sql);
        jdbcTemplate.execute(sql);
        return true;
    }
}
